const fs = require('fs');
const events = require('events');
const path = require('path');

async function recursiveDir(directory, onFile, onDir) {
  // 递归遍历文件夹
  async function listDir(dir, currentPaths) {
    for await (const dirent of dir) {
      if (dirent.isDirectory()) {
        let stop = await onDir && onDir(dirent.name, currentPaths);
        if (false === stop) {
          continue;
        }
        await listDir(
          await fs.promises.opendir(path.join(...currentPaths, dirent.name)),
          currentPaths.concat(dirent.name)
        );
      } else if (dirent.isFile()) {
        await onFile && onFile(dirent.name, currentPaths);
      }
    }
  }
  const dir = await fs.promises.opendir(directory);
  listDir(dir, [dir.path]);
}

recursiveDir('.', function(filename, paths) {
  console.log('File:', paths.join('/'), filename);
}, function onDir(dirname, paths) {
  if (dirname.startsWith('.')) {
    return false;
  }
  console.log(' Dir:', paths.join('/'), dirname);
});